package weaver.interfaces.zhlx.duss.action.hrmresourcemodule.hcm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.HcmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-12-04
 * @Description: 新员工培养转正流程调用接口异动流程传输SAP
 * @ModifyLog:
 **/
public class WeaXygpyZzlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaXygpylcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用HCM系统接口返回:";
            RecordSet rs = new RecordSet();
            HcmUtil hcmUtil = new HcmUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String gh = Util.null2String(rs.getString("gh"));
            String zzsj = Util.null2String(rs.getString("zzsj"));
            String czlx = Util.null2String(rs.getString("czlx"));
            String khlb = Util.null2String(rs.getString("khlb"));
            String zj = Util.null2String(rs.getString("zj"));
            String srl = Util.null2String(rs.getString("srl"));
            JSONArray dataList = new JSONArray();
            JSONObject dataJson = new JSONObject();
            dataJson.put("UUID", hcmUtil.getUUID16Str());
            dataJson.put("PERNR", gh);
            dataJson.put("BEGDA", hcmUtil.formatDate(zzsj));
            dataJson.put("MASSN", czlx);
            dataJson.put("MASSG", khlb);
            dataJson.put("ZZGWZD", zj);
            dataJson.put("ZHR_SRL", srl);
            dataJson.put("ZZSYZT", "21".equals(khlb) ? "20" : "30");
            dataList.add(dataJson);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaXygpylcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaXygpylcAction>>>>>dataList=" + dataList);
            JSONObject resultJson = hcmUtil.handleOKHttpRequest("EC201", dataList, dataMap);
            writeLog(">>>>>WeaXygpylcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if (!"S".equals(msgType)) {
                    message += outputJson.getString("MSG_TEXT");
                    String returnData = outputJson.getString("DATA");
                    JSONArray returnDataList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnDataList.size(); i++) {
                        String type = returnDataList.getJSONObject(i).getString("TYPE");
                        if("E".equalsIgnoreCase(type)){
                            message += returnDataList.getJSONObject(i).getString("MESSAGE");
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXygpylcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}